package com.xh.poster.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.awt.*;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

/**
 * @Project: normal-core
 * @Package: com.mydataway.mall.system.qrcode.poster.core.utils
 * @Author: little rookie
 * @CreateDate: 2021/3/20 10:15
 * @Version: 1.0
 * @Description:
 * @Company:
 */
@Slf4j
public class FontUtil {

    public static final String TTF_FONT_FILE_PATH = "static/fonts/apple_simple.ttf";

    /**
     * ttf字体文件网络路径
     */
    public static final String TTF_FONT_FILE_NETWORK_PATH = "https://c-platform-prod.s3.cn-northwest-1.amazonaws.com.cn/basic-mall-config/apple_simple.ttf";

    private FontUtil() {
    }

    private static Font font;

    public static Font getFont() {
        if (Objects.isNull(font)) {
            synchronized (FontUtil.class) {
                if (Objects.nonNull(font)) {
                    return font;
                }
                InputStream is = null;
                try {
                    is = FontUtil.getLocalFileInputStreamClasspath();
//                    is = FontUtil.getNetworkFileInputStream();
                    if (Objects.isNull(is)) {
                        log.error("【获取文件流数据为空】");
                        throw new IllegalArgumentException("获取文件流数据为空");
                    }
                    font = Font.createFont(Font.TRUETYPE_FONT, is);
                } catch (Exception e) {
                    log.error("【初始化字体对象出错】：", e);
                } finally {
                    if (Objects.nonNull(is)) {
                        try {
                            is.close();
                        } catch (Exception e) {
                            log.error("【初始化字体对象关流出错】：【{}】", e.getMessage());
                        }
                    }
                }
            }
        }
        return font;
    }

    /*读取本地文件-1*/
    private static InputStream getLocalFileInputStreamClasspath() {
        //文件名
        InputStream stream = null;
        try {
            ClassPathResource classPathResource = new ClassPathResource(TTF_FONT_FILE_PATH);
            stream = classPathResource.getInputStream();
        } catch (Exception e) {
            log.error("【获取本地文件出错】：", e);
        }
        return stream;
    }

    /*读取本地文件-2*/
    private static InputStream getLocalFileInputStreamPath() {
        return FontUtil.class.getResourceAsStream(TTF_FONT_FILE_PATH);
    }

    /*读取网络文件*/
    private static InputStream getNetworkFileInputStream() {
        URL url = null;
        try {
            url = new URL(TTF_FONT_FILE_NETWORK_PATH);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            return conn.getInputStream();
        } catch (Exception e) {
            log.error("【获取网络文件出错】：", e);
        }
        return null;
    }

}
